(function() {
  var express, iced, pgsql, router, secure, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  express = require('express');

  pgsql = require('pg');

  secure = require('../lib/secure');

  router = express.Router();

  module.exports = router;

  router.get('/', function(req, res, next) {
    return res.render('upgrade');
  });

  router.post('/do', function(req, res, next) {
    var client, cnt, done, err, mer, result, trctl, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/upgrade.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 14
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          done(client);
          return next(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/upgrade.coffee"
          });
          client.query("select * from pay_merch where uuid in ( select t60_merid from pay_p2 )", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 20
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            done(client);
            return next(err);
          }
          cnt = result.rows.length;
          (function(__iced_k) {
            var _i, _len, _ref, _results, _while;
            _ref = result.rows;
            _len = _ref.length;
            _i = 0;
            _while = function(__iced_k) {
              var _break, _continue, _next;
              _break = __iced_k;
              _continue = function() {
                return iced.trampoline(function() {
                  ++_i;
                  return _while(__iced_k);
                });
              };
              _next = _continue;
              if (!(_i < _len)) {
                return _break();
              } else {
                mer = _ref[_i];
                trctl = mer.trctl;
                trctl.trcodes.t22 = true;
                (function(__iced_k) {
                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                    parent: ___iced_passed_deferral,
                    filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/routes/upgrade.coffee"
                  });
                  client.query("update pay_merch set trctl = $1 where uuid = $2", [JSON.stringify(trctl), mer.uuid], __iced_deferrals.defer({
                    assign_fn: (function() {
                      return function() {
                        err = arguments[0];
                        return result = arguments[1];
                      };
                    })(),
                    lineno: 32
                  }));
                  __iced_deferrals._fulfill();
                })(function() {
                  if (err) {
                    done(client);
                    return next(err);
                  }
                  return _next();
                });
              }
            };
            _while(__iced_k);
          })(function() {
            done();
            return res.render('upgrade_done', {
              cnt: cnt
            });
          });
        });
      };
    })(this));
  });

}).call(this);
